
package info.anska;

//import com.facebook.react.bridge.ReactApplicationContext;
//import com.facebook.react.bridge.ReactContextBaseJavaModule;
//import com.facebook.react.bridge.ReactMethod;
//import com.facebook.react.bridge.Callback;
//
//public class RNFileSelectModule extends ReactContextBaseJavaModule {
//
//  private final ReactApplicationContext reactContext;
//
//  public RNFileSelectModule(ReactApplicationContext reactContext) {
//    super(reactContext);
//    // this.reactContext = reactContext;
//  }
//
//  @Override
//  public String getName() {
//    return "RNFileSelect";
//  }
//}


import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;
import android.os.Environment;
import com.facebook.react.bridge.ActivityEventListener;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import java.util.regex.Pattern;
import android.net.Uri;
import android.util.Log;
import java.io.File;
import info.anska.utils;
public class RNFileSelectModule extends ReactContextBaseJavaModule {
  public static final int PERMISSIONS_REQUEST_CODE = 0;
  public static final int FILE_PICKER_REQUEST_CODE = 1;
  private Callback onDone;
  private Callback onCancel;

  public RNFileSelectModule(ReactApplicationContext reactContext) {
    super(reactContext);

    getReactApplicationContext().addActivityEventListener(new ActivityEventListener());
  }

  @Override
  public String getName() {
    return "RNFileSelector";
  }

  @ReactMethod
  public void Show(final ReadableMap props, final Callback onDone, final Callback onCancel) {
    boolean openFilePicker = checkPermissionsAndOpenFilePicker();
    if (openFilePicker) {
      openFilePicker1(props, onDone, onCancel);
    }
  }

  private boolean checkPermissionsAndOpenFilePicker() {
    String permission = Manifest.permission.WRITE_EXTERNAL_STORAGE;
    if (ContextCompat.checkSelfPermission(getCurrentActivity(), permission) != PackageManager.PERMISSION_GRANTED) {
      if (ActivityCompat.shouldShowRequestPermissionRationale(getCurrentActivity(), permission)) {
        showError();
        return false;
      } else {
        ActivityCompat.requestPermissions(getCurrentActivity(), new String[] { permission }, PERMISSIONS_REQUEST_CODE);
        return true;
      }
    }

    return true;
  }

  private void showError() {
    Toast.makeText(getCurrentActivity(), "Allow external storage reading", Toast.LENGTH_SHORT).show();
  }

  private void openFilePicker1(final ReadableMap props, final Callback onDone, final Callback onCancel) {
    final Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    Activity currentActivity = getCurrentActivity();
    Uri uri = Uri.parse(Environment.getDataDirectory().getPath());
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setDataAndType(uri,"*/*");
    currentActivity.startActivityForResult(intent.createChooser(intent,"请选择文件"), 1);
    this.onDone = onDone;
    this.onCancel = onCancel;
  }

  private class ActivityEventListener implements com.facebook.react.bridge.ActivityEventListener {

    @Override
    public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
      if(requestCode==1&&resultCode==AppCompatActivity.RESULT_OK){
          Uri uri=data.getData();
          utils.getPath(activity,uri);
          String path=utils.getPath(activity,uri);
          if(onDone!=null){
          onDone.invoke(path);
          }
          onDone = null;
        }else if(requestCode == 1 && resultCode == AppCompatActivity.RESULT_CANCELED){
          if (onCancel != null) {
            onCancel.invoke();
          }
          onCancel = null;
        }
    }
    @Override
    public void onNewIntent(Intent intent) {

    }
  }
}
